var utils = require('/utils/index.js');
var base = require('/base_info.js');
Page({
  data: {
    roomNum: ['1', '2', '3'],
    roomNumIndex: 0,
    arriveTime: ['15:00', '17:00', '20:00'],
    arriveTimeIndex: 0,
    userName: '',
    userPhone: '',
    roomInfo: {},
    inTime: '2018-01-01',
    outTime: '2018-01-01',
    imgUrl: base.imgUrl
  },
  // 房间数量选择器改变函数
  roomNumChange (e) {
    this.setData({
      roomNumIndex: e.detail.value,
    });
  },
  // 到店时间选择器改变函数
  arriveTimeChange (e) {
    this.setData({
      arriveTimeIndex: e.detail.value,
    });
  },
  // 监听入住人姓名修改
  bindUserName (e) {
    this.setData({
      userName: e.detail.value,
    });
  },
  formSubmit: function(e) {
    this.pay(e.detail.formId);
  },
  // 监听入住人手机号码修改
  bindUserPhone(e) {
    this.setData({
      userPhone: e.detail.value,
    });
  },
  // 点击去结算调起支付
  pay (formId) {
    // 创建支付订单
    let that = this;
    let roomInfo = that.data.roomInfo;
    utils.getData('AliTradeOrder', {
      buyer_id: my.getStorageSync({
        key: 'user_id',
      }).data,
      user_id: my.getStorageSync({
        key: 'uid',
      }).data,
      seller_id: base.seller_id,
      total_cost: that.data.roomInfo.price * 1 * that.data.roomNum[that.data.roomNumIndex],
      room_id: roomInfo.id,
      seller_name: roomInfo.name,
      arrival_time: my.getStorageSync({
        key: 'inTime',
      }).data,
      departure_time: my.getStorageSync({
        key: 'outTime',
      }).data,
      dd_time: that.data.arriveTimeIndex,
      num: that.data.roomNumIndex,
      price: roomInfo.price,
      days: that.data.lightLong,
      name: that.data.userName,
      dis_cost: that.data.roomInfo.price * 1 * that.data.roomNum[that.data.roomNumIndex],
      dis_cost: 0.01,
      from_id: formId,
      room_logo: that.data.roomInfo.logo,
      tel: that.data.userPhone
    }, res => {
      console.log(res);
      // 调用支付
      my.tradePay({
        tradeNO: res.data.data.trade_no,
        success: (res) => {
          if (res.resultCode == 9000) {
            my.alert({
              content: '支付成功'
            });
          } else {
            my.alert({
              content: '支付失败'
            });
          }
        },
        fail: (res) => {
          my.alert({
            content: '支付失败',
          });
        }
      });
    }, err => {
      my.alert({
        content: '支付失败'
      });
    });
  },
  onLoad(query) {
    // 页面加载
    console.info(my.getStorageSync({
      key: 'inTime',
    }).data);
    let that = this;
    // 页面显示
    // 获取缓存时间
    let inTime = my.getStorageSync({
      key: 'inTime',
    }).data;
    let outTime = my.getStorageSync({
      key: 'outTime',
    }).data;
    let lightLong = (new Date(outTime)) - (new Date(inTime));
    this.setData({
      inTime: inTime,
      outTime: outTime,
      lightLong: lightLong / 86400000
    });
    utils.getData('SingleRoomDetails', {
      room_id: query.id
    }, (res) => {
      that.setData({
        roomInfo: res.data.data
      });
      console.log(that.data.roomInfo);
    }, (err) => {
      console.log(err);
    });
  },
  detail(e) {
    console.log(e);
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
